package com.sudo.service;

import com.github.pagehelper.PageInfo;
import com.sudo.common.enums.UserParams;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.common.utils.RandomString;
import com.sudo.common.utils.RedisOperator;
import com.sudo.pojo.vo.SysMenuButtonVO;
import com.sudo.pojo.vo.SysRoleButtonVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author：为道日损 2020-04-25 23:45
 * webSite：https://www.sudo.ren
 */
@Service
public class BaseService {
    @Autowired
    private RedisOperator redisOperator;

    public PagedGridResult setterPagedGrid(List<?> list, Integer page) {
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult gridResult = new PagedGridResult();
        gridResult.setPage(page);
        gridResult.setRows(list);
        gridResult.setTotal(pageList.getPages());
        gridResult.setRecords(pageList.getTotal());
        return gridResult;
    }

    public String getRoleCode(){
        return RandomString.getRandomString(8);
    }

    public String getMenuCode(){
        return RandomString.getRandomString(8);
    }

    public String getButtonCode(){
        return RandomString.getRandomString(8);
    }

    protected boolean hasStrInListStr(String str,String listStr) {
        String[] strArr = StringUtils.strip(listStr, "[]").split(",");
        List<String> strList = Arrays.asList(strArr);
        return strList.contains(str);
    }

    protected void setRedisMenuButtonToRedis(String menuCode ,String buttonCode) {
        String key = UserParams.REDIS_MENU_BUTTON.value + ":" + menuCode;
        redisOperator.sadd(key, buttonCode);
    }

    protected void setRedisMenuButtonToRedis(List<SysMenuButtonVO> sysMenuButtonVOList) {
        for (SysMenuButtonVO t:sysMenuButtonVOList){
            String key = UserParams.REDIS_MENU_BUTTON.value + ":" + t.getMenuCode();
            redisOperator.sadd(key, t.getButtonCode());
        }
    }

    protected void delRedisMenuButtonFromRedis(String menuCode ,String buttonCode) {
        String key = UserParams.REDIS_MENU_BUTTON.value + ":" + menuCode;
        if (null == buttonCode) {
            redisOperator.del(key);
        }else {
            redisOperator.srem(key, buttonCode);
        }
    }

    protected void setRedisRolePermissionToRedis(String roleCode, String menuCode, String buttonCode) {
        String key = UserParams.REDIS_ROLE_PERMISSION.value + ":" + roleCode + ":" + menuCode;
        redisOperator.sadd(key, buttonCode);
    }

    protected void setRedisRolePermissionToRedis(List<SysRoleButtonVO> sysRoleButtonVOList) {
        for (SysRoleButtonVO t:sysRoleButtonVOList){
            String key = UserParams.REDIS_ROLE_PERMISSION.value + ":" + t.getRoleCode() + ":" + t.getMenuCode();
            if (!redisOperator.hasKey(key)) {
                redisOperator.sadd(key, UserParams.NONE.value);
            }else{
                redisOperator.sadd(key, t.getButtonCode());
            }
        }
    }

    protected void delRedisRolePermissionFromRedis(String roleCode, String menuCode,String buttonCode) {
        String key = UserParams.REDIS_ROLE_PERMISSION.value + ":" + roleCode + ":" + menuCode;
        if (null == buttonCode) {
            redisOperator.del(key);
        }else {
            redisOperator.srem(key, buttonCode);
        }
    }

    protected void delRedisRolePermissionFromRedis(String roleCode) {
        String key = UserParams.REDIS_ROLE_PERMISSION.value + ":" + roleCode + ":*";
        redisOperator.mdel(redisOperator.keys(key));
    }

    protected void setRedisRoleUserToRedis(String roleCode, String username) {
        String key = UserParams.REDIS_ROLE_USER.value + ":" + roleCode;
        redisOperator.sadd(key, username);
    }

    protected void updateRedisRoleForUser(String username, String oldRoleCode, String newRoleCode) {
        String oldKey = UserParams.REDIS_ROLE_USER.value + ":" + oldRoleCode;
        String newKey = UserParams.REDIS_ROLE_USER.value + ":" + newRoleCode;
        redisOperator.srem(oldKey, username);
        redisOperator.sadd(newKey, username);
    }

    protected void delRedisRoleUserFromRedis(String roleCode, String username) {
        String key = UserParams.REDIS_ROLE_USER.value + ":" + roleCode;
        if (null == username) {
            redisOperator.del(key);
        } else {
            redisOperator.srem(key, username);
        }
    }

    protected void delRedisUserTokenFromRedis(String username) {
        String key = UserParams.REDIS_USER_TOKEN.value + ":" + username;
        redisOperator.del(key);
    }

    /**
     * 通过username从redis中获取roleCode
     * @param username 用户名
     * @return string
     */
    protected String getRoleCodeFromRedisByUsername(String username) {
        if (null == username) {
            return null;
        }
        Set<String> keys = redisOperator.keys(UserParams.REDIS_ROLE_USER.value + ":*");
        String roleCode = null;
        for (String key : keys) {
            boolean flag = redisOperator.sismember(key, username);
            if (flag){
                roleCode = key.replace(UserParams.REDIS_ROLE_USER.value+":","");
                break;
            }
        }
        return roleCode;
    }
}
